package cn.yanhu.kuwanapp.earn.fragment

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import cn.woochen.comlib.BaseFragment
import cn.woochen.comlib.adapter.OnItemClickListener
import cn.yanhu.kuwanapp.R
import cn.yanhu.kuwanapp.bean.response.RespGameListBean
import cn.yanhu.kuwanapp.common.viewmodels.CommonViewModel
import cn.yanhu.kuwanapp.databinding.FragmentEarnGameListBinding
import cn.yanhu.kuwanapp.earn.GameViewModel
import cn.yanhu.kuwanapp.earn.adapter.EarnGameListAdapter
import cn.yanhu.kuwanapp.home.MainViewModel
import cn.yanhu.kuwanapp.home.fragment.GameListFragment
import cn.yanhu.kuwanapp.utils.Config
import cn.yanhu.kuwanapp.utils.NavigatorHelper
import cn.yanhu.kuwanapp.utils.constans.Constants
import cn.yanhu.kuwanapp.utils.ext.parseState
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import jfq.wowan.com.myapplication.PlayMeUtil

/**
 *
 * 类描述：赚钱-游戏列表
 * @author：woochen
 * create at：2021/7/27 10:38 PM
 */
class EarnGameListFragment:BaseFragment<FragmentEarnGameListBinding, GameViewModel>() {

    private val mGameListDatas by lazy {
        mutableListOf<RespGameListBean>()
    }

    private val mGameListAdapter by lazy {
        EarnGameListAdapter(mGameListDatas)
    }

    override fun onResume() {
        super.onResume()
        requestData()
    }

    override fun setContentView(): Int = R.layout.fragment_earn_game_list

    private var page = 1

    override fun registerNecessaryObserver() {
        super.registerNecessaryObserver()
        mViewModel.gameListInfo.observe(this, Observer { it ->
            parseState(it,{
                 if (page == 1){
                     mGameListDatas.clear()
                 }else{
                     if (it.isEmpty()) mBinding.refreshLayout.finishLoadMoreWithNoMoreData() else mBinding.refreshLayout.finishLoadMore()
                 }
                 mGameListDatas.addAll(it)
                 if (mGameListDatas.isEmpty()) showEmpty()
                 mGameListAdapter.notifyDataSetChanged()
            })
        })
    }

    override fun initData() {
        initWidgets()
    }
    private val mCommonViewModel by viewModels<CommonViewModel>()

    private fun initWidgets() {
        mBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                requestData()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
               page = 1
                refreshLayout.finishRefresh()
                requestData()
            }
        })
        mBinding.rvGame.run {
            adapter = mGameListAdapter
            mGameListAdapter.mItemClickListener = OnItemClickListener {
                mCommonViewModel.eventTrace(Constants.EVENT_KEY_APP_HOME_GAMELIST_CLICK)
                val gameBean = mGameListDatas[it]
                NavigatorHelper.toCpl(mContext,gameBean.id)
//                NavigatorHelper.toCpl(mContext,gameBean.detailUrl)
            }
        }
    }

    override fun requestData() {
        mViewModel.gameList("1",mIndex,page)
    }

    private val mIndex by lazy {
        arguments?.getInt(EXTRA_INDEX)?:0
    }


    companion object{
        private const val EXTRA_INDEX = "extra_index"
        fun newInstance(index: Int) = EarnGameListFragment().apply {
            arguments = Bundle().apply {
                putInt(EXTRA_INDEX,index)
            }
        }
    }
}